package com.huizhonglm.diap.ui.user;

import android.content.Intent;
import android.os.Bundle;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

import com.bruce.common.ActivityMgr;
import com.bruce.common.utils.EmptyUtil;
import com.bruce.common.utils.PatternUitl;
import com.huizhonglm.diap.R;
import com.huizhonglm.diap.core.utils.ToastUtil;
import com.huizhonglm.diap.presenters.user.LoginPresenter;
import com.huizhonglm.diap.ui.BaseActivity;
import com.huizhonglm.diap.ui.tab.TabhostActivity;
import com.huizhonglm.diap.ui.user.iView.LoginIView;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-03 09:47
 */
public class LoginActivity extends BaseActivity implements LoginIView, View.OnClickListener {

    private EditText accEdt;
    private EditText pwdEdt;
    private ImageButton showPwdIbtn;
    private boolean isShowPwd = false;

    private LoginPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMgr.getInstance().pushActivity(LoginActivity.this);
        setContentView(R.layout.activity_login);
        setBackEvent(this, null);
        mPresenter = new LoginPresenter(this);
        assignViews();
    }

    @Override
    protected void assignViews(){
        accEdt = (EditText) findViewById(R.id.login_account_edt);
        pwdEdt = (EditText) findViewById(R.id.login_passwd_edt);
        View loginBtn = findViewById(R.id.login_btn);
        View registerBtn = findViewById(R.id.register_btn);
        View forgetPwdBtn = findViewById(R.id.forget_pwd_btn);
        showPwdIbtn = (ImageButton) findViewById(R.id.show_passwd_ibtn);
        loginBtn.setOnClickListener(this);
        registerBtn.setOnClickListener(this);
        forgetPwdBtn.setOnClickListener(this);
        showPwdIbtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login_btn:
                login();
                break;
            case R.id.register_btn:
                startActivity(new Intent(this, InputPhoneNumActivity.class));
                break;
            case R.id.forget_pwd_btn:
                startActivity(new Intent(this, PasswdForgetActivity.class));
                break;
            case R.id.show_passwd_ibtn:
                togglePasswd();
                break;
        }
    }

    @Override
    public void onLoginSuccess() {
        hideProgressBar();
        ToastUtil.makeTextShort("登录成功");
        startActivity(new Intent(this, TabhostActivity.class));
        finish();
    }

    @Override
    public void onLoginFailure(String msg) {
        hideProgressBar();
        ToastUtil.makeTextShort(msg);
    }

    private void login(){
        String account = accEdt.getText().toString().trim();
        String passwd = pwdEdt.getText().toString().trim();
        if (EmptyUtil.isEmpty(account)){
            ToastUtil.makeTextShort("请输入登录账号");
            return;
        } else {
            if (account.contains("@")) {
                if (!PatternUitl.isEmail(account)) {
                    ToastUtil.makeTextShort("请输入有效的邮箱地址");
                    return;
                }
            } else {
                if (!PatternUitl.isPhone(account)) {
                    ToastUtil.makeTextShort("请输入有效的手机号码");
                    return;
                }
            }
        }
        if (EmptyUtil.isEmpty(passwd)){
            ToastUtil.makeTextShort("请输入登录密码");
            return;
        }
        showProgressBar();
        mPresenter.login(account, passwd);
    }

    private void togglePasswd(){
        if (isShowPwd) {
            //设置EditText文本为隐藏的
            showPwdIbtn.setImageResource(R.drawable.ic_show_passed_n);
            pwdEdt.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
            //设置EditText文本为可见的
            showPwdIbtn.setImageResource(R.drawable.ic_show_passed_s);
            pwdEdt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
        isShowPwd = !isShowPwd;
        pwdEdt.postInvalidate();
        //切换后将EditText光标置于末尾
        CharSequence pwdText = pwdEdt.getText();
        if (pwdText != null) {
            Spannable spanText = (Spannable) pwdText;
            Selection.setSelection(spanText, pwdText.length());
        }
    }
}
